package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberComp;
import org.hgvc.webapp.ets.bo.MemberInd;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.jsf.beans.agent.AdminCommissionBean;
import org.hgvc.webapp.ets.jsf.beans.member.MemberAgreementTransferBean;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class AdminMemberMgmtBean extends AbstractBean {

	private String name;
	private String regno;
	private MemberService service;
	private Member member;
	private MemberInd memberInd;
	private MemberComp memberComp;
	private List<Member> memberList;
	
	public static final String BEAN_NAME="adminMemberMgmtBean";
	public static final String NAV_LIST="adminMemberMgmt";
	
	public String terminateAction(){
		return AdminAgreementTerminateBean.NAV_LIST.concat("_"+getRoleName());
	}
	
	public void terminateActionListener(ActionEvent actionEvent) throws BaseException, Exception{
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("amemberId3");
		String id =(String)param.getValue();
		AdminAgreementTerminateBean bean = (AdminAgreementTerminateBean)FacesUtils.getManagedBean(AdminAgreementTerminateBean.BEAN_NAME);
		bean.iniBeanForPreTermination((Member)commonDao.getObject(Member.class, id));
		
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRegno() {
		return regno;
	}
	public void setRegno(String regno) {
		this.regno = regno;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public MemberInd getMemberInd() {
		return memberInd;
	}
	public void setMemberInd(MemberInd memberInd) {
		this.memberInd = memberInd;
	}
	public MemberComp getMemberComp() {
		return memberComp;
	}
	public void setMemberComp(MemberComp memberComp) {
		this.memberComp = memberComp;
	}
	
	public void setService(MemberService service) {
		this.service = service;
	}
	/*---------------------------------------------------------------------*/
	public List<Member> getMemberList() {
		try{
			memberList = service.getMemberActiveList();
			memberList = service.searchRegNoPartnerNameInList(regno, memberList);
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return memberList;
	}
		
	public String searchAction() throws Exception{
		
		
		return NAV_LIST.concat("_"+getRoleName());
	}
	
	public String backAction(){
		return AdminPortalBean.NAV_ADMIN_PORTAL;
	}
	
	public String navListAction(){
		return NAV_LIST.concat("_"+getRoleName());
	}
	
	
	public void editMemberActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("cmemberId");
		String id =(String)param.getValue();
		for(Member member:memberList){
			if(member.getMemberId().equalsIgnoreCase(id)){
				this.member = member;
				break;
			}
		}
	}
	
	public String editMemberAction(){
		if(member.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_COMP)){
			AdminMemberCompProfileBean bean = (AdminMemberCompProfileBean)FacesUtils.getManagedBean(AdminMemberCompProfileBean.BEAN_NAME);
			return bean.navViewAction(member);
		}else{
			AdminMemberIndProfileBean bean = (AdminMemberIndProfileBean)FacesUtils.getManagedBean(AdminMemberIndProfileBean.BEAN_NAME);
			return bean.navViewAction(member);
		}
	}
	
	public void viewAgreementActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("amemberId");
		String id =(String)param.getValue();
		for(Member member:memberList){
			if(member.getMemberId().equalsIgnoreCase(id)){
				this.member = member;
				break;
			}
		}
	}
	
	public String viewAgreementAction(){
		AdminMemberAgreementMgmtBean bean = (AdminMemberAgreementMgmtBean)FacesUtils.getManagedBean(AdminMemberAgreementMgmtBean.BEAN_NAME);
		try{
		if (member.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_IND)){
			MemberInd memberInd = this.service.getMemberIndByMemberId(member.getMemberId());
			member.setName(memberInd.getNameAsIcPassport());
		}else{
			MemberComp memberComp = this.service.getMemberCompByMemberId(member.getMemberId());
			member.setName(memberComp.getCompName());
		}
		bean.setMember(member);
		}catch(BaseException be){
			initErrorBean(be);
		}
		return bean.navMemberAgreementMgmt(member);
	}
// added by jimmy for agreement transfer
	public void transferActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("amemberId2");
		String id =(String)param.getValue();
		for(Member member:memberList){
			if(member.getMemberId().equalsIgnoreCase(id)){
				this.member = member;
				break;
			}
		}
	}
	
	public String transferAction(){
		MemberAgreementTransferBean bean = (MemberAgreementTransferBean)FacesUtils.getManagedBean(MemberAgreementTransferBean.BEAN_NAME);
		try{
		if (member.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_IND)){
			MemberInd memberInd = this.service.getMemberIndByMemberId(member.getMemberId());
			member.setName(memberInd.getNameAsIcPassport());
		}else{
			MemberComp memberComp = this.service.getMemberCompByMemberId(member.getMemberId());
			member.setName(memberComp.getCompName());
		}
			bean.setFromMember(member);
		}catch(BaseException be){
			initErrorBean(be);
		}
		bean.ini();
		return bean.NAV_LIST.concat("_"+getRoleName());
	}

}
